package zucc.edu.cn.chengchengparttime.LoginAndReg;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

import java.util.ArrayList;

import zucc.edu.cn.chengchengparttime.MainActivity;
import zucc.edu.cn.chengchengparttime.R;
import zucc.edu.cn.chengchengparttime.User;
import zucc.edu.cn.chengchengparttime.Work;
import zucc.edu.cn.chengchengparttime.dbHomeHelper;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText input_account;
    private EditText input_password;
    private AppCompatButton btn_login_account;
    private AppCompatButton btn_login_phone;
    private AppCompatButton btn_register;
    dbHomeHelper homeHelper; //数据库帮助类

    private void initView() {
        //初始化控件
        input_account=(EditText) findViewById(R.id.input_account);
        input_password=(EditText) findViewById(R.id.input_password);
        btn_login_account=(AppCompatButton) findViewById(R.id.btn_login_account);
        btn_login_phone=(AppCompatButton) findViewById(R.id.btn_login_phone);
        btn_register=(AppCompatButton) findViewById(R.id.btn_register);

        //设置点击事件监听器
        btn_login_account.setOnClickListener(this);
        btn_login_phone.setOnClickListener(this);
        btn_register.setOnClickListener(this);
    }

    public static String currentUser="User1";
    public static String getCurrentUser() {
        return currentUser;
    }
    public static void setCurrentUser(String currentUser) {
        LoginActivity.currentUser = currentUser;
    }

    private SQLiteDatabase mDatabase;// 数据库实例

    private static final String DEBUG_TAG="Chengcheng Log"; //调试标志

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);

        initView();
        homeHelper=new dbHomeHelper(this,"Chengcheng.db",null,1);
        mDatabase=homeHelper.getWritableDatabase();

        btn_login_account.setOnClickListener(this); //设置监听事件
        btn_login_phone.setOnClickListener(this);
        btn_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_register: //注册
                Toast.makeText(this, "点击了注册" , Toast.LENGTH_SHORT).show();
                //跳转到注册界面
                Intent intent=new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.btn_login_phone:
                Toast.makeText(this, "点击了手机号登录" , Toast.LENGTH_SHORT).show();
                //跳转到手机号登录界面
                Intent intent1=new Intent(LoginActivity.this, LoginPhoneActivity.class);
                startActivity(intent1);
                break;
            //登录验证
            case R.id.btn_login_account:
                Toast.makeText(this, "点击了账号密码登录" , Toast.LENGTH_SHORT).show();

                String UserID = input_account.getText().toString().trim();
                String Password = input_password.getText().toString().trim();
                if (!TextUtils.isEmpty(UserID) && !TextUtils.isEmpty(Password)) {
                    ArrayList<User> data = dbHomeHelper.getAllData(mDatabase);
                    boolean match = false;
                    for (int i = 0; i < data.size(); i++) {
                        User user = data.get(i);
                        if ((UserID.equals(user.getUserID()) && Password.equals(user.getPassword()))) {
                            this.currentUser=UserID;
                            match = true;
                            break;
                        } else {
                            match = false;
                        }
                    }
                    if (match) {
                        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                        Runnable target;
                        //用线程启动
                        Thread thread = new Thread(){
                            @Override
                            public void run(){
                                try {
                                    sleep(2000);//2秒 模拟登录时间
                                    Intent intent3 = new Intent();
                                    intent3.setClass(LoginActivity.this, MainActivity.class);
                                    //一定要指定是第几个pager，这里填写1
                                    intent3.putExtra("id", 1);
                                    startActivity(intent3);
                                    finish();
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }
                        };
                        thread.start();//打开线程
                    } else {
                        Toast.makeText(this, "请重新输入正确的账号和密码", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "请输入你的用户名或密码", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
